package edu.bupt.c1array;

import org.apache.directory.api.util.Strings;

public class Test {
    private static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }

    public ListNode oddEvenList(ListNode head) {

        if(null==head || null == head.next) return head;

        ListNode odd = head;
        ListNode oddTail = head;
        ListNode even = head.next;
        ListNode evenTail = head.next;

        ListNode index = even.next;
        boolean flag = true;
        while(null != index){
            if (flag){
                oddTail.next = index;
                oddTail = index;
            }else {
                evenTail.next = index;
                evenTail = index;
            }
            index = index.next;
            flag = !flag;
        }

        oddTail.next = even;
        evenTail.next = null;
        return odd;
    }

    public ListNode reverseBetween(ListNode head, int m, int n) {
        // 提供头节点
        ListNode demo = new ListNode(-1,null);
        demo.next = head;
        for(int i=m;i>1;i--){
            demo = demo.next;
        }
        // 头插法嘛~~~~~~~
        ListNode index = demo.next.next;
        ListNode pre=demo.next;
        for(int i=m+1;i<=n;i++){
            pre.next = index.next;
            index.next = demo.next;
            demo.next = index;
            index = pre.next;
        }
        return head;

    }

    // String - 3
    public int myAtoi(String str) {
        String trim = Strings.trim(str);
        boolean positive = true;
        int sum = 0;
        int i = 0;
        if ('-' == (trim.charAt(0))){
            positive = false;
            i++;
        }else{

            for (;i<trim.length();i++){
                char c = trim.charAt(i);
                if ('0'<=c && c <= '9'){
                    sum = sum*10 + (c -'0');
                }else{
                    break;
                }
            }
        }
        return positive?sum:-sum;
    }

    public static void main(String[] args) {
        /*ListNode listNode5 = new ListNode(5, null);
        ListNode listNode4 = new ListNode(3, listNode5);
        ListNode listNode = new Test().reverseBetween(listNode4,1,2);
        System.out.println(listNode);*/
        int i = new Test().myAtoi("   -40");

    }


}
